package com.sun.converter;

import com.sun.entities.Department;
import com.sun.entities.Employee;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * String 类型转换为Employee类型
 * @author sun
 */
@Component
public class StringToEmployeeConverter implements Converter<String,Employee> {

    @Override
    public Employee convert(String source) {
        if(source!=null){
            String[] strings = source.split("-");
            if(strings.length == 4){
                String lastName=strings[0];
                String email = strings[1];
                Integer gender = Integer.parseInt(strings[2]);
                Integer deptId = Integer.parseInt(strings[3]);
                Department dept = new Department();
                dept.setId(deptId);
                Employee employee = new Employee(null,lastName,email,gender,dept);
                System.out.println(source+"--converter--"+employee);
                return employee ;
            }
        }
        return null;
    }
}
